package com.xiaomazi.statistics.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.xiaomazi.common.utils.R;
import com.xiaomazi.statistics.client.UcenterClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaomazi.statistics.entity.StatisticsDaily;
import com.xiaomazi.statistics.mapper.StatisticsDailyMapper;
import com.xiaomazi.statistics.service.StatisticsDailyService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService{

    @Autowired
    private UcenterClient ucenterClient;
    /**
     * 统计某一天注册人数,生成统计数据
     * @param day 日期
     * @return ok
     */
    @Override
    public R registerCount(String day) {
        this.update().eq("date_calculated",day).remove();
        Integer count = ucenterClient.getCountByDay(day);
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(count);
        statisticsDaily.setDateCalculated(day);

        statisticsDaily.setVideoViewNum(RandomUtil.randomInt(100,200));
        statisticsDaily.setLoginNum(RandomUtil.randomInt(100,200));
        statisticsDaily.setCourseNum(RandomUtil.randomInt(100,200));
        this.save(statisticsDaily);

        return R.ok();
    }

    /**
     * 图表显示，返回两部分数据，日期json数组，数量json数组
     * @param type  类型
     * @param begin 开始时间
     * @param end   结束时间
     * @return XData YData
     */
    @Override
    public R showData(String type, String begin, String end) {

        List<StatisticsDaily> statisticsDailyList = this.query()
                .between("date_calculated", begin, end)
                .select("date_calculated",type)
                .list();

        List<String> date_calculatedList = statisticsDailyList.stream()
                .map(StatisticsDaily::getDateCalculated)
                .collect(Collectors.toList());

        List<Integer> numDataList = new ArrayList<>();
        statisticsDailyList.stream().forEach(item -> {
            switch (type) {
                case "login_num":
                    numDataList.add(item.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(item.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(item.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(item.getCourseNum());
                    break;
                default:
                    break;
            }
        });

        //把封装之后两个list集合放到map集合，进行返回
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numDataList",numDataList);
        return R.ok().data(map);
    }
}
